package com.foxmail.aroundme.youtubedemo.listview;

import android.app.FragmentManager;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;

import com.foxmail.aroundme.youtubedemo.R;
import com.foxmail.aroundme.youtubedemo.VideoFragment;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;

import java.util.List;

/**
 * Created by gzl on 2016/10/25.
 */

public class ListViewAdapter extends BaseAdapter {
    private List<String> list;
    private Context context;
    private FragmentManager fragmentManager;
    private ListViewHolder currentHolder = null;
    private VideoFragment videoFragment;

    public ListViewAdapter(Context context, List<String> list, FragmentManager fragmentManager) {
        this.list = list;
        this.context = context;
        this.fragmentManager = fragmentManager;
        videoFragment = VideoFragment.getInstance();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        final ListViewHolder viewHolder = new ListViewHolder(convertView);

        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.lastViewHolder = currentHolder;
                currentHolder = viewHolder;
                if(viewHolder.lastViewHolder != null){
                    setViewHide(viewHolder.lastViewHolder);
                }
                setViewShow(viewHolder);
                fragmentManager.beginTransaction().remove(videoFragment).commitAllowingStateLoss();
                fragmentManager.executePendingTransactions();
                fragmentManager.beginTransaction().add(viewHolder.getId(), videoFragment).commitAllowingStateLoss();
                videoFragment.play(list.get(position));
            }
        });


        return convertView;
    }
    private void setViewHide(ListViewHolder holder){
        holder.button.setVisibility(View.VISIBLE);
        holder.changeFrame.setVisibility(View.VISIBLE);
        holder.videoFrame.setVisibility(View.GONE);
    }

    private void setViewShow(ListViewHolder holder){
        holder.button.setVisibility(View.GONE);
        holder.changeFrame.setVisibility(View.GONE);
        holder.videoFrame.setVisibility(View.VISIBLE);
    }

    class ListViewHolder {
        FrameLayout videoFrame;
        FrameLayout changeFrame;
        Button button;
        ListViewHolder lastViewHolder;
        private int id;

        public ListViewHolder(View view) {
            videoFrame = (FrameLayout) view.findViewById(R.id.video);
            changeFrame = (FrameLayout) view.findViewById(R.id.frame);
            button = (Button) view.findViewById(R.id.btn);
            id = hashCode();
            videoFrame.setId(id);
        }
        public int getId() {
            return id;
        }
    }
}
